package cn.sjtu.netty.bytebuffer;

import cn.sjtu.netty.util.ByteBufferUtil;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

/**
 * @author syo 2319615164@qq.com
 * @description 读取ByteBuffer
 * @date 2024/6/21
 * @Copyright syo
 */
public class TestReadByteBuffer {
    public static void main(String[] args) {
        // 1. 从buffer读向buffer中写
        ByteBuffer buffer = ByteBuffer.wrap("hello world".getBytes(StandardCharsets.UTF_8));
        try (FileChannel channel = new FileOutputStream("test02.txt").getChannel()) {
            // 此时已经是读模式了，wrap方法会自动转换为读模式
            channel.write(buffer);
        } catch (IOException e) {
        }

        // 2. 直接get
        ByteBuffer buffer1 = ByteBuffer.allocate(10);
        buffer1.put((byte) 0x61);
        buffer1.flip();
        buffer1.get();
    }
}
